package com.bike.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bike.entity.HhAgent;
import com.bike.entity.HhBusiness;
import com.bike.entity.HhBusinessRecord;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhPayRecord;
import com.bike.entity.HhWithdraw;
import com.bike.mapper.HhAgentMapper;
import com.bike.mapper.HhBusinessMapper;
import com.bike.mapper.HhBusinessRecordMapper;
import com.bike.mapper.HhPayRecordMapper;
import com.bike.mapper.HhWithdrawMapper;
import com.bike.util.Constant;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.util.TransFerUtil;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

@Service
public class HhWithdrawServiceImpl extends BaseServcieImpl  {

	@Autowired
	HhWithdrawMapper hhWithdrawMapper;
	@Autowired
	HhBusinessMapper hhBusinessMapper;
	@Autowired
	HhBusinessRecordServiceImpl hhBusinessRecordServiceImpl;
	@Autowired
	HhBusinessRecordMapper hhBusinessRecordMapper;
	@Autowired
	HhAgentMapper hhAgentMapper;
	@Autowired
	HhAgentServiceImpl hhAgentServiceImpl;
	@Autowired
	HhPayRecordMapper hhPayRecordMapper;
	@Autowired
	HhPayRecordServiceImpl hhPayRecordServiceImpl;
	
	/**
	* 保存
	*/
	public void saveOrUpdate(PageData pd, HhWithdraw record) {
		if(pd.getLong("withdrawId")>0){
			 //编辑
			hhWithdrawMapper.updateByPrimaryKeySelective(record);
		}else{
			hhWithdrawMapper.insertSelective(record);
		}
	}
	
	/**
	* 删除
	*/
	public int deleteHhWithdrawById(long withdrawId) {
		return hhWithdrawMapper.deleteByPrimaryKey(withdrawId);
	}
	
	/**
	*列表分页
	*/
	public PageData selectHhWithdrawList(PageData pd) {
		PageData result=new PageData();
		PageBounds pageBounds=(PageBounds) pd.get("pageB");
		String advanceFilter=pd.getString("advanceFilter");
		if(advanceFilter!=null&&!advanceFilter.equals("")){
			advanceFilter=advanceFilter.replaceAll("'", "");
			JSONArray array=JSONObject.parseArray(advanceFilter);
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<array.size();i++){
				JSONObject object=array.getJSONObject(i);
				String field=object.getString("field");
				String op=object.getString("op");//equal  contains  notequal  greater  greaterorequal  less  lessorequal  beginwith  endwith
				String value=object.getString("value");
				buffer.append(" and ").append(TransFerUtil.getMybatisField(field)).append(TransFerUtil.getMybatisOperation(op,value));
			}
			if(array.size()>0){
				pd.put("condition", buffer.toString());
			}
		}
		List<HhWithdraw> records= hhWithdrawMapper.selectByPage(pd,pageBounds);
		result.put("rows",records );
		result.put("total", ((PageList<HhWithdraw>) records).getPaginator().getTotalCount());
		return result;
	}
	 
	/**
	*列表(全部)
	*/
	public List<HhWithdraw> listAll(PageData pd) {
		return  hhWithdrawMapper.selectAll(pd);
	}
	
	/**
	* 通过id获取数据
	*/
	public HhWithdraw selectByHhWithdrawId(Long withdrawId) {
		return hhWithdrawMapper.selectByPrimaryKey(withdrawId);
	}

	public void updateWithdraw(PageData pd, HhWithdraw hhWithdraw) {
		//改变提现状态
		hhWithdraw.setStatus(1);
		int result=hhWithdrawMapper.updateWithdraw(hhWithdraw);
		if(result<=0){
			throw new RuntimeException("提现失败");
		}
		
		//增加消费记录
		//系统平台代理
		HhAgent agent= hhAgentServiceImpl.insertOrSelectByPrimaryKey();
				
		//提现完成后 把商家  进行中的提现记录  服务费提现记录  标记为完成
		PageData busRecordPd=new PageData();
		if(hhWithdraw.getType()==0){
			hhBusinessRecordMapper.updateToSuccess(hhWithdraw);
		}else{
			
			hhPayRecordMapper.updateToSuccess(hhWithdraw);
		}
		
		 //更新系统平台代理商余额
		 agent.setBalance(agent.getBalance().subtract(hhWithdraw.getAmount()));
		 hhAgentMapper.updateByPrimaryKeySelective(agent);
		
		 hhPayRecordServiceImpl.insertAgentPayRecord(agent, hhWithdraw.getAmount(), Constant.platform, "用户提现完成提现金额",false);
		
	}

	public int insertBusinessWithdraw(HhBusinessUser businessUser) {

		HhBusiness business=hhBusinessMapper.selectByPrimaryKey(businessUser.getBusinessId());
		
		BigDecimal serviceFee=null;
		BigDecimal trueAccount=business.getBalance();//实际到账
		if(business.getPayWithdraw()==0){
			serviceFee=business.getBalance().multiply(new BigDecimal(Constant.WXwithdrawFee));
			trueAccount=business.getBalance().subtract(serviceFee);
		}
		//提现表
		HhWithdraw withdraw=new HhWithdraw();
		withdraw.setAddTime(TimeManager.currentTime());
		withdraw.setBusinessId(business.getBusinessId());
		withdraw.setBusinessName(business.getName());
		withdraw.setUserBusinessId(businessUser.getBusinessUserId());
		withdraw.setUserBusinessName(businessUser.getName());
		withdraw.setWithdrawAmount(business.getBalance());
		withdraw.setPoundage(new BigDecimal("2"));
		withdraw.setServiceAmount(serviceFee);
		withdraw.setAmount(trueAccount);
		withdraw.setBank(business.getBank());
		withdraw.setBankCode(business.getBankCode());
		withdraw.setBankBranch(business.getBankBranch());
		withdraw.setBankUser(business.getBankName());
		return hhWithdrawMapper.insertSelective(withdraw);
		
	}

	}

